package com.clz.appdesign.appdependence.baseKotlin

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.clz.appdesign.appdependence.widget.LoadingDialog
import com.trello.rxlifecycle2.components.RxFragment
import rx.subscriptions.CompositeSubscription

/**
 * @author chenliangzhi
 * @date 2018/1/10
 * @describe
 */

abstract class BaseFragment : RxFragment() {
    protected var compositeSubscription: CompositeSubscription? = null
    protected var loadingDialog: LoadingDialog? = null
    private var mContextView: View? = null

    override fun onSaveInstanceState(outState: Bundle) {
        outState.putBoolean(STATE_SAVE_IS_HIDDEN, isHidden)
        super.onSaveInstanceState(outState)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        beforeInit()
        compositeSubscription = CompositeSubscription()
        // “内存重启”时调用  解决重叠问题  #0001
        if (savedInstanceState != null) {
            val isSupportHidden = savedInstanceState.getBoolean(STATE_SAVE_IS_HIDDEN)
            val ft = fragmentManager.beginTransaction()
            if (isSupportHidden) {
                ft.hide(this)
            } else {
                ft.show(this)
            }
            ft.commit()
        }
    }

    /**
     * 界面初始化前期准备
     */
    protected open fun beforeInit() {

    }

    fun showLoading() {
        if (loadingDialog == null) {
            loadingDialog = LoadingDialog(activity)
        }
        if (!loadingDialog!!.isShowing()) {
            loadingDialog!!.show()
        }
    }

    fun hideLoading() {
        if (loadingDialog != null && loadingDialog!!.isShowing()) {
            loadingDialog!!.dismiss()
        }
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle): View? {
        mContextView = inflater.inflate(bindLayout(), container, false)
        initView(mContextView)
        doBusiness(activity)
        return mContextView
    }

    /**
     * [绑定布局]
     *
     * @return
     */
    abstract fun bindLayout(): Int

    /**
     * [初始化控件]
     *
     * @param view
     */
    abstract fun initView(view: View?)

    /**
     * [业务操作]
     *
     * @param mContext
     */
    abstract fun doBusiness(mContext: Context)

    /**
     * 日志输出
     *
     * @param msg
     */
    fun TLog(msg: String) {
        if (AppUtils.isAppDebug()) {
            LogUtils.d(msg)
        }
    }

    fun toast(text: String) {
        ToastUtils.showShort(text)
    }

    override fun onDestroyView() {
        super.onDestroyView()
        if (compositeSubscription != null && !compositeSubscription!!.isUnsubscribed) {
            compositeSubscription!!.unsubscribe()
        }
        if (loadingDialog != null) {
            loadingDialog = null
        }
    }

    companion object {
        private val STATE_SAVE_IS_HIDDEN = "STATE_SAVE_IS_HIDDEN"
    }
}
